package com.profound.notes.main.slice;

import com.profound.notes.component.SpanLayout;
import com.profound.notes.component.SpanLayoutManager;
import com.profound.notes.component.WaterfallLayoutManager;
import com.profound.notes.main.slice.vm.NoteViewModel;
import com.profound.notes.main.slice.vm.TODOViewModel;
import com.profound.notes.mvvm.ViewModelProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * @author zjwujlei
 * @date 2021/4/24
 * @email zjwujlei@sina.com
 * @description
 */
public class PlanePageSliderProvider extends PageSliderProvider {

    private AbilitySlice mOwner;
    ViewModelProvider mProvider;
    private NoteViewModel mNote;
    private TODOViewModel mTODO;
    private LayoutScatter scatter;

    private Context mContext;

    public PlanePageSliderProvider(ViewModelProvider provider, AbilitySlice owner) {
        mOwner = owner;
        mProvider = provider;
        mNote = provider.get(NoteViewModel.class);
        mTODO = provider.get(TODOViewModel.class);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        System.out.println("createPageInContainer:" + i);
        if (scatter == null) {
            scatter = LayoutScatter.getInstance(componentContainer.getContext());
        }
        if (i == 0) {
            return createNotePage(componentContainer);
        } else if (i == 1) {
            return createTODOPage(componentContainer);
        }
        return null;
    }

    public Object createNotePage(ComponentContainer componentContainer) {
        if (scatter == null) {
            scatter = LayoutScatter.getInstance(componentContainer.getContext());
        }
        ListContainer mLayout = new ListContainer(componentContainer.getContext());
        mLayout.setLayoutConfig(new ListContainer.LayoutConfig(ListContainer.LayoutConfig.MATCH_PARENT, ListContainer.LayoutConfig.MATCH_PARENT));
        mLayout.setItemProvider(new NoteItemProvider(mProvider, mOwner));
        componentContainer.addComponent(mLayout);
        return mLayout;
    }

    public Object createTODOPage(ComponentContainer componentContainer) {
        if (scatter == null) {
            scatter = LayoutScatter.getInstance(componentContainer.getContext());
        }
        SpanLayout mLayout = new SpanLayout(componentContainer.getContext());
        mLayout.setLayoutConfig(new SpanLayout.LayoutConfig(SpanLayout.LayoutConfig.MATCH_PARENT, SpanLayout.LayoutConfig.MATCH_PARENT));
        mLayout.setItemProvider(new NoteItemProvider(mProvider, mOwner));
        mLayout.setLayoutManager(new WaterfallLayoutManager(2));
        componentContainer.addComponent(mLayout);
        return mLayout;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }
}
